/**
 * 用户管理
 */
var pageCurr;
var form;
$(function() {
    layui.use('table', function(){
        var table = layui.table;
        form = layui.form;

        tableIns=table.render({
            elem: '#category2List',
            url:'/category2/getCategory2List',
            method: 'post', //默认：get请求
            cellMinWidth: 80,
            page: true,
            request: {
                pageName: 'pageNum', //页码的参数名称，默认：pageNum
                limitName: 'pageSize' //每页数据量的参数名，默认：pageSize
            },
            response:{
                statusName: 'code', //数据状态的字段名称，默认：code
                statusCode: 200, //成功的状态码，默认：0
                countName: 'totals', //数据总数的字段名称，默认：count
                dataName: 'list' //数据列表的字段名称，默认：data
            },
            cols: [[
                {type:'numbers'}
                ,{field:'category2Name', title:'服务名',align:'center'}
                ,{field:'category1Name', title:'所属类型',align:'center'}
                ,{field:'category2Remark', title:'描述',align:'center'}
                ,{field:'recommendedPrice', title: '价格',align:'center'}
                ,{field:'bcreateTime', title: '创建时间',align:'center'}
                ,{field:'bupdateTime', title: '更新时间',align:'center'}
                ,{field:'category2Label', title: '是否有效',align:'center'}
                ,{title:'操作',align:'center', toolbar:'#optBar'}
            ]],
            done: function(res, curr, count){
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                //console.log(res);
                //得到当前页码
                console.log(curr);
                $("[data-field='category2Label']").children().each(function(){
                    if($(this).text()=='0'){
                        $(this).text("有效")
                    }else if($(this).text()=='1'){
                        $(this).text("失效")
                    }
                });
                //得到数据总量
                //console.log(count);
                pageCurr=curr;
            }
        });

        //监听工具条
        table.on('tool(category2Table)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                //删除
                delCategory2(data,data.category2Id);
            } else if(obj.event === 'edit'){
                //编辑
                openCategory2(data,"编辑");
            }else if(obj.event === 'recover'){
                //恢复
                recoverCategory2(data,data.category2Id);
            }
        });

        //监听提交
        form.on('submit(category2Submit)', function(data){
            // TODO 校验
            formSubmit(data);
            return false;
        });
    });

    //搜索框
    layui.use(['form','laydate'], function(){
        var form = layui.form ,layer = layui.layer
            ,laydate = layui.laydate;
        ///////
        var src='<option value="">请选择...</option>';
        $.ajax({
            url:'/category1/getCategory1s',
            dataType:'json',
            async: true,
            success:function(data){
                $.each(data,function(index,item){
                    if(!category10Id){
                        var option = new Option(item.category1Name,item.category1Id);
                    }else {
                        var option = new Option(item.category1Name,item.category1Id);
                        // // 如果是之前的parentId则设置选中
                        if(item.category1Id == category10Id) {
                            option.setAttribute("selected",'true');
                        }
                    }
                    $('#category10Id').append(option);//往下拉菜单里添加元素
                    form.render('select'); //这个很重要
                })
            }
        });

        ////////
        //TODO 数据校验
        //监听搜索框
        form.on('submit(searchSubmit)', function(data){
            //重新加载table
            load(data);
            return false;
        });
    });
});

//提交表单
function formSubmit(obj){
    $.ajax({
        type: "POST",
        data: $("#category2Form").serialize(),
        url: "/category2/setCategory2",
        success: function (data) {
            if (data.code == 1) {
                layer.alert(data.msg,function(){
                    layer.closeAll();
                    load(obj);
                });
            } else {
                layer.alert(data.msg);
            }
        },
        error: function () {
            layer.alert("操作请求错误，请您稍后再试",function(){
                layer.closeAll();
                //加载load方法
                load(obj);//自定义
            });
        }
    });
}

//开通用户
function addCategory2(){
    openCategory2(null,"新增服务");
}
function openCategory2(data,title){
    var category10Id0 = null;
    if(data==null || data==""){
        $("#category2Id").val("");
    }else{
        $("#category2Id").val(data.category2Id);
        $("#category2Name0").val(data.category2Name);
        $("#category2Remark").val(data.category2Remark);
        $("#recommendedPrice").val(data.recommendedPrice);
        category10Id0 = data.category10Id;
    }
    var pageNum = $(".layui-laypage-skip").find("input").val();
    $("#pageNum").val(pageNum);


    $.ajax({
        url:'/category1/getCategory1s',
        dataType:'json',
        async: true,
        success:function(data){
            $("#category10Id0").empty();

            $.each(data,function(index,item){
               /* if(!category10Id0){
                    var option = new Option(item.category1Name,item.category1Id);
                }else {*/
                    var option = new Option(item.category1Name,item.category1Id);
                    // // 如果是之前的parentId则设置选中
                    if(item.category1Id == category10Id0) {
                        option.setAttribute("selected",'true');
                    }
                //}

                $('#category10Id0').append(option);//往下拉菜单里添加元素
                form.render('select'); //这个很重要

            })
        }
    });


    layer.open({
        type:1,
        title: title,
        fixed:false,
        resize :false,
        shadeClose: true,
        area: ['550px','500px'],
        content:$('#setCategory2'),
        end:function(){
            cleanCategory2();
        }
    });
}

function delCategory2(obj,category2Id) {
    if(null!=category2Id){
        layer.confirm('您确定要删除吗？', {
            btn: ['确认','返回'] //按钮
        }, function(){
            $.post("/category2/updateCategory2Status",{"category2Id":category2Id,"category2Label":1},function(data){
                if (data.code == 1) {
                    layer.alert(data.msg,function(){
                        layer.closeAll();
                        load(obj);
                    });
                } else {
                    layer.alert(data.msg);
                }
            });
        }, function(){
            layer.closeAll();
        });
    }
}
//恢复
function  recoverCategory2(obj,category2Id) {
    if(null!=category2Id){
        layer.confirm('您确定要恢复吗？', {
            btn: ['确认','返回'] //按钮
        }, function(){
            $.post("/category2/updateCategory2Status",{"category2Id":category2Id,"category2Label":0},function(data){
                if (data.code == 1) {
                    layer.alert(data.msg,function(){
                        layer.closeAll();
                        load(obj);
                    });
                } else {
                    layer.alert(data.msg);
                }
            });
        }, function(){
            layer.closeAll();
        });
    }
}

function load(obj){
    //重新加载table
    tableIns.reload({
        where: obj.field
        , page: {
            curr: pageCurr //从当前页码开始
        }
    });
}

function cleanCategory2(){
    $("#category2Name0").val("");
    $("#category2Remark").val("");
    $("#recommendedPrice").val("");

}
